#include "iu_manage.h"
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>

// 更新时间显示
void buy_tickets_screen_init(void);
void pick_up_screen_init(void);
void create_image_slideshow(void);

lv_obj_t * menu_screen        = NULL; // 菜单屏幕对象
static lv_obj_t * label_time  = NULL; // 时间标签
static lv_obj_t * film_list   = NULL; // 影片列表
static lv_obj_t * notice_area = NULL; // 公告区域

// 添加一个标志来跟踪menu_screen是否被隐藏
bool menu_screen_hidden = false;

// 添加全局定时器句柄
lv_timer_t * time_timer = NULL;

// 动态公告数组和计数
static char ** notice_texts = NULL;
static int notice_count     = 0;

// 动态电影名称数组和计数
static char ** film_names = NULL;
static int film_count     = 0;

// 添加从notices.txt文件加载公告的函数
static bool load_notices_from_file(void)
{
    FILE * file = fopen("notices.txt", "r");
    if(!file) {
        LV_LOG_USER("Failed to open notices.txt for reading");
        return false;
    }

    // 首先计算文件中有多少行
    int count = 0;
    char line[256];
    while(fgets(line, sizeof(line), file)) {
        count++;
    }
    rewind(file); // 重置文件指针到开头

    // 为公告文本分配内存（先释放旧的内存）
    if(notice_texts) {
        // 释放之前的内存
        for(int i = 0; i < notice_count; i++) {
            free(notice_texts[i]);
        }
        free(notice_texts);
    }

    notice_texts = malloc(count * sizeof(char *));
    if(!notice_texts) {
        fclose(file);
        return false;
    }

    // 初始化公告数组
    notice_count = 0;

    // 读取文件内容
    while(fgets(line, sizeof(line), file) && notice_count < count) {
        // 移除行尾的换行符
        line[strcspn(line, "\r\n")] = 0;

        // 复制公告文本
        notice_texts[notice_count] = malloc(strlen(line) + 1);
        if(notice_texts[notice_count]) {
            strcpy(notice_texts[notice_count], line);
            LV_LOG_USER("Added notice: %s", notice_texts[notice_count]);
            notice_count++;
        }
    }

    fclose(file);
    LV_LOG_USER("Loaded %d notices from file", notice_count);
    return notice_count > 0;
}

// 加载电影列表 movie.txt前12部电影
static bool load_films_from_file(void)
{
    FILE * file = fopen("movie.txt", "r");
    if(!file) {
        LV_LOG_USER("Failed to open movie.txt for reading");
        return false;
    }

    // 首先计算文件中有多少行，但最多只处理12行
    int count = 0;
    char line[256];
    while(fgets(line, sizeof(line), file) && count < 12) {
        count++;
    }
    rewind(file); // 重置文件指针到开头

    // 为电影名称分配内存
    if(film_names) {
        // 释放之前的内存
        for(int i = 0; i < film_count; i++) {
            free(film_names[i]);
        }
        free(film_names);
    }

    film_names = malloc(count * sizeof(char *));
    if(!film_names) {
        fclose(file);
        return false;
    }

    // 初始化电影名称数组
    film_count = 0;
    char movie_name[100];
    int movie_id;
    char duration[20];
    int price;

    // 读取文件内容，最多读取12部电影 (格式: 电影编号,电影名,电影时长,电影票单价)
    while(fgets(line, sizeof(line), file) && film_count < 12) {
        // 移除行尾的换行符
        line[strcspn(line, "\r\n")] = 0;

        // 解析行数据: 电影编号,电影名,电影时长,电影票单价
        if(sscanf(line, "%d,%99[^,],%19[^,],%d", &movie_id, movie_name, duration, &price) == 4) {

            LV_LOG_USER("Parsed movie: id=%d, name=%s, duration=%s, price=%d", movie_id, movie_name, duration, price);

            // 复制电影名称
            film_names[film_count] = malloc(strlen(movie_name) + 1);
            if(film_names[film_count]) {
                strcpy(film_names[film_count], movie_name);
                LV_LOG_USER("Added movie to list: %s", film_names[film_count]);
                film_count++;
            }
        } else {
            LV_LOG_USER("Failed to parse line: %s", line);
        }
    }

    fclose(file);
    LV_LOG_USER("Loaded %d movies from file (limited to 12)", film_count);
    return film_count > 0;
}

// 添加释放电影名称内存的函数
static void free_films(void)
{
    if(film_names) {
        for(int i = 0; i < film_count; i++) {
            free(film_names[i]);
        }
        free(film_names);
        film_names = NULL;
        film_count = 0;
    }
}

// 添加释放公告内存的函数
static void free_notices(void)
{
    if(notice_texts) {
        for(int i = 0; i < notice_count; i++) {
            free(notice_texts[i]);
        }
        free(notice_texts);
        notice_texts = NULL;
        notice_count = 0;
    }
}
// 更新时间显示
// void buy_tickets_update_time_timer(lv_timer_t * e)
// {
//     // 安全检查
//     if(!e || !label_time || !menu_screen) return;

//     // 获取当前系统时间
//     time_t now;
//     struct tm * t;
//     time(&now);
//     t = localtime(&now);
//     if(!t) return;

//     char time_str[64];
//     // 星期几转换
//     const char * weekday[] = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
//     snprintf(time_str, sizeof(time_str), "%s   %d年%d月%d日   %02d:%02d:%02d", 
//              weekday[t->tm_wday], t->tm_year + 1900,
//              t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
//     lv_label_set_text(label_time, time_str);
// }
// 更新时间显示
void buy_tickets_update_time_timer(lv_timer_t * e)
{
    // 安全检查
    if(!e || !label_time || !menu_screen) return;

    // 获取当前系统时间
    time_t now;
    struct tm * t;
    time(&now);
    
    // 检查time()函数是否成功
    if(now == (time_t)-1) {
        lv_label_set_text(label_time, "时间获取失败");
        return;
    }
    
    t = localtime(&now);
    // 检查localtime()函数是否成功
    if(!t) {
        lv_label_set_text(label_time, "时间格式化失败");
        return;
    }

    char time_str[128]; // 增加缓冲区大小以防万一
    // 星期几转换
    const char * weekday[] = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
    
    // 检查tm_wday的值是否在有效范围内
    if(t->tm_wday < 0 || t->tm_wday > 6) {
        lv_label_set_text(label_time, "日期数据异常");
        return;
    }
    
    int result = snprintf(time_str, sizeof(time_str), "%s   %d年%d月%d日   %02d:%02d:%02d", 
             weekday[t->tm_wday], t->tm_year + 1900,
             t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
    
    // 检查snprintf是否成功
    if(result < 0 || result >= sizeof(time_str)) {
        lv_label_set_text(label_time, "时间字符串格式化失败");
        return;
    }
    
    lv_label_set_text(label_time, time_str);
}
// 影片推荐项点击事件
static void film_item_click_handler(lv_event_t * e)
{
    lv_obj_t * item = lv_event_get_target(e);
    if(lv_event_get_code(e) == LV_EVENT_CLICKED) {
        // 修复API使用错误
        lv_obj_t * label = lv_obj_get_child(item, 0);
        if(label && lv_obj_check_type(label, &lv_label_class)) {
            const char * film_name = lv_label_get_text(label);
            LV_LOG_USER("Selected film: %s", film_name);
        }
        // 这里可以添加跳转到影片详情或购票的逻辑
        // buy_tickets_screen_init(film_name); // 假设有这个函数
    }
}

// 功能按钮点击事件
static void function_btn_click_handler(lv_event_t * e)
{
    lv_obj_t * btn = lv_event_get_target(e);
    if(lv_event_get_code(e) == LV_EVENT_CLICKED) {
        lv_obj_t * label = lv_obj_get_child(btn, 0);
        if(!label || !lv_obj_check_type(label, &lv_label_class)) return;

        const char * btn_text = lv_label_get_text(label);

        if(strcmp(btn_text, "自助购票") == 0) {
            LV_LOG_USER("Navigating to ticket purchase");
            // 暂停定时器
            if(time_timer) {
                lv_timer_pause(time_timer);
            }

            // 隐藏当前界面
            if(menu_screen) {
                lv_obj_add_flag(menu_screen, LV_OBJ_FLAG_HIDDEN);
                menu_screen_hidden = true;
            }
            // 创建或显示电影购票页面
            buy_tickets_screen_init_new();

        } else if(strcmp(btn_text, "自助取票") == 0) { // 添加缺失的取票功能
            LV_LOG_USER("Navigating to ticket pickup");
            // 暂停定时器
            if(time_timer) {
                lv_timer_pause(time_timer);
            }

            // 隐藏当前界面
            if(menu_screen) {
                lv_obj_add_flag(menu_screen, LV_OBJ_FLAG_HIDDEN);
                menu_screen_hidden = true;
            }
            // 创建或显示取票界面
            pick_up_screen_init();

        } else if(strcmp(btn_text, "退出登录") == 0) { // 修改按钮文本匹配
            LV_LOG_USER("Returning to first screen");
            // 暂停定时器
            if(time_timer) {
                lv_timer_pause(time_timer);
            }

            // 隐藏当前界面
            if(menu_screen) {
                lv_obj_add_flag(menu_screen, LV_OBJ_FLAG_HIDDEN);
                menu_screen_hidden = true;
            }
            // 创建或显示新界面
            create_image_slideshow();
        }
    }
}

// 创建通用功能按钮
static lv_obj_t * create_function_button(lv_obj_t * parent, const char * text, lv_coord_t x, lv_coord_t y, lv_coord_t w,
                                         lv_coord_t h)
{
    lv_obj_t * btn = lv_btn_create(parent);
    if(!btn) return NULL;

    lv_obj_set_size(btn, w, h);
    lv_obj_align(btn, LV_ALIGN_CENTER, x, y);
    lv_obj_set_style_bg_color(btn, lv_color_hex(0x0099ff), 0);
    lv_obj_set_style_bg_opa(btn, LV_OPA_COVER, 0);
    lv_obj_set_style_radius(btn, 8, 0);
    lv_obj_set_style_text_color(btn, lv_color_hex(0xffffff), 0);

    lv_obj_clear_flag(btn, LV_OBJ_FLAG_SCROLLABLE);

    lv_obj_t * label = lv_label_create(btn);
    if(label) {
        lv_obj_set_style_text_font(label, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
        lv_label_set_text(label, text);
        lv_obj_center(label);
    }

    lv_obj_add_event_cb(btn, function_btn_click_handler, LV_EVENT_ALL, NULL);
    lv_obj_add_flag(btn, LV_OBJ_FLAG_CLICKABLE);

    return btn;
}

// 创建标题栏
static void create_title_section(lv_obj_t * parent, const char * title)
{
    lv_obj_t * title_bar = lv_obj_create(parent);
    if(!title_bar) return;

    lv_obj_set_size(title_bar, lv_pct(100), 35);
    lv_obj_set_style_bg_color(title_bar, lv_color_hex(0xf0f0f0), 0);
    lv_obj_set_style_bg_opa(title_bar, LV_OPA_COVER, 0);
    lv_obj_set_style_border_width(title_bar, 0, 0);
    lv_obj_clear_flag(title_bar, LV_OBJ_FLAG_SCROLLABLE); // 确保标题栏不滚动

    lv_obj_t * title_label = lv_label_create(title_bar);
    if(title_label) {
        lv_obj_set_style_text_font(title_label, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
        lv_obj_set_style_text_color(title_label, lv_color_hex(0x333333), 0);
        lv_label_set_text(title_label, title);
        lv_obj_align(title_label, LV_ALIGN_LEFT_MID, 15, 0);
    }
}

// 创建今日推荐影片区域
static lv_obj_t * create_film_section(lv_obj_t * parent)
{
    lv_obj_t * film_container = lv_obj_create(parent);
    if(!film_container) return NULL;

    lv_obj_set_size(film_container, 470, 440);
    lv_obj_set_style_bg_color(film_container, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_border_width(film_container, 1, 0);
    lv_obj_set_style_border_color(film_container, lv_color_hex(0xeeeeee), 0);
    lv_obj_clear_flag(film_container, LV_OBJ_FLAG_SCROLLABLE); // 取消滚动
    lv_obj_set_layout(film_container, LV_LAYOUT_FLEX);         // Flex布局
    lv_obj_set_flex_flow(film_container, LV_FLEX_FLOW_COLUMN); // 垂直布局
    lv_obj_set_style_border_width(film_container, 0, 0);       // 去掉边框
    lv_obj_set_style_pad_all(film_container, 0, 0);            // 设置内边距

    // 添加标题栏（不滚动）
    lv_obj_t * title_bar = lv_obj_create(film_container);
    lv_obj_set_size(title_bar, lv_pct(100), 35);                     // 宽度占满
    lv_obj_set_style_bg_color(title_bar, lv_color_hex(0xf0f0f0), 0); // 背景颜色
    lv_obj_set_style_bg_opa(title_bar, LV_OPA_COVER, 0);             // 透明度 100%
    lv_obj_set_style_border_width(title_bar, 0, 0);                  // 去掉边框
    lv_obj_clear_flag(title_bar, LV_OBJ_FLAG_SCROLLABLE);            // 取消滚动

    lv_obj_t * title_label = lv_label_create(title_bar);
    lv_obj_set_style_text_font(title_label, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
    lv_obj_set_style_text_color(title_label, lv_color_hex(0x333333), 0);
    lv_label_set_text(title_label, "今日推荐影片");
    lv_obj_align(title_label, LV_ALIGN_LEFT_MID, 15, 0);

    // 创建可滚动的影片列表区域
    lv_obj_t * list_container = lv_obj_create(film_container);
    lv_obj_set_size(list_container, lv_pct(100), lv_pct(100)); // 占据剩余空间
    lv_obj_set_style_bg_opa(list_container, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(list_container, 0, 0);
    lv_obj_add_flag(list_container, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_flex_grow(list_container, 1); // 自动填充剩余空间

    // 在可滚动区域中创建影片列表
    film_list = lv_list_create(list_container);
    if(film_list) {
        // 设置列表样式
        lv_obj_set_style_bg_color(film_list, lv_color_hex(0xffffff), LV_PART_MAIN);
        lv_obj_set_style_border_width(film_list, 0, LV_PART_MAIN);
        lv_obj_set_style_pad_all(film_list, 10, LV_PART_MAIN);
        lv_obj_set_style_pad_row(film_list, 5, LV_PART_MAIN); // 列表项之间的间距

        // 设置列表尺寸和位置
        lv_obj_set_size(film_list, lv_pct(100), LV_SIZE_CONTENT);
        lv_obj_align(film_list, LV_ALIGN_TOP_LEFT, 0, 0);
        lv_obj_set_style_bg_opa(film_list, LV_OPA_TRANSP, 0);

        // 添加影片项
        // 首先尝试从文件加载电影名称
        if(!load_films_from_file()) {
            LV_LOG_USER("Failed to load films from file, using default films");
            // 如果加载失败，使用默认的电影名称
            static const char * default_films[] = {"阿凡达: 水之道", "九龙城寨", "逃出白垩纪", "挑战", "万里归途",
                                                   "阿凡达: 水之道", "九龙城寨", "逃出白垩纪", "挑战", "万里归途"};
            for(int i = 0; i < sizeof(default_films) / sizeof(default_films[0]); i++) {
                lv_obj_t * item = lv_list_add_btn(film_list, LV_SYMBOL_VIDEO, default_films[i]);
                if(item) {
                    lv_obj_set_style_text_font(item, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
                    lv_obj_set_style_border_width(item, 0, 0);
                    lv_obj_set_style_bg_color(item, lv_color_hex(0xf5f5f5), 0);
                    lv_obj_set_style_radius(item, 5, 0);
                    lv_obj_set_style_pad_all(item, 10, 0);
                    lv_obj_set_style_pad_bottom(item, 5, 0);
                    lv_obj_add_event_cb(item, film_item_click_handler, LV_EVENT_ALL, NULL);
                }
            }
        } else {
            // 使用从文件加载的电影名称
            for(int i = 0; i < film_count; i++) {
                lv_obj_t * item = lv_list_add_btn(film_list, LV_SYMBOL_VIDEO, film_names[i]);
                if(item) {
                    lv_obj_set_style_text_font(item, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
                    lv_obj_set_style_border_width(item, 0, 0);
                    lv_obj_set_style_bg_color(item, lv_color_hex(0xf5f5f5), 0);
                    lv_obj_set_style_radius(item, 5, 0);
                    lv_obj_set_style_pad_all(item, 10, 0);
                    lv_obj_set_style_pad_bottom(item, 5, 0);
                    lv_obj_add_event_cb(item, film_item_click_handler, LV_EVENT_ALL, NULL);
                }
            }
        }
    }

    return film_container;
}
// 创建活动公告区域
lv_obj_t * create_notice_section(lv_obj_t * parent)
{
    // 主容器（包含标题栏和内容区）
    lv_obj_t * notice_container = lv_obj_create(parent);
    if(!notice_container) return NULL;

    lv_obj_set_size(notice_container, 310, 150); // 总高度150px
    lv_obj_set_style_bg_color(notice_container, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_border_width(notice_container, 1, 0);
    lv_obj_set_style_border_color(notice_container, lv_color_hex(0xeeeeee), 0);
    lv_obj_clear_flag(notice_container, LV_OBJ_FLAG_SCROLLABLE); // 主容器不滚动
    lv_obj_set_layout(notice_container, LV_LAYOUT_FLEX);         // 使用Flex布局
    lv_obj_set_flex_flow(notice_container, LV_FLEX_FLOW_COLUMN); // 垂直排列
    lv_obj_set_style_pad_all(notice_container, 0, 0);            // 设置内边距

    // 1. 标题栏（不滚动、背景不透明）
    lv_obj_t * title_bar = lv_obj_create(notice_container);
    lv_obj_set_size(title_bar, lv_pct(100), 30);                     // 标题栏高度30px
    lv_obj_set_style_bg_color(title_bar, lv_color_hex(0xf0f0f0), 0); // 不透明背景
    lv_obj_set_style_bg_opa(title_bar, LV_OPA_100, 0);               // 完全不透明
    lv_obj_set_style_border_width(title_bar, 0, 0);
    lv_obj_clear_flag(title_bar, LV_OBJ_FLAG_SCROLLABLE); // 标题栏不滚动

    // 标题文本
    lv_obj_t * title_label = lv_label_create(title_bar);
    lv_label_set_text(title_label, "活动公告");
    lv_obj_set_style_text_font(title_label, &SourceHanSansCN_Light_2_16_all, 0);
    lv_obj_center(title_label); // 文字居中

    // 2. 公告内容区（可滚动）
    lv_obj_t * content_container = lv_obj_create(notice_container);
    lv_obj_set_size(content_container, lv_pct(100), 120);         // 明确设置高度为120px（150-30）
    lv_obj_set_style_bg_opa(content_container, LV_OPA_TRANSP, 0); // 透明背景
    lv_obj_set_style_border_width(content_container, 0, 0);
    lv_obj_add_flag(content_container, LV_OBJ_FLAG_SCROLLABLE); // 内容区可滚动
    lv_obj_set_style_pad_all(content_container, 10, 0);         // 添加内边距

    // 公告内容文本
    lv_obj_t * notice_label = lv_label_create(content_container);
    lv_obj_set_style_text_font(notice_label, &SourceHanSansCN_Light_2_16_all, 0);
    lv_obj_set_width(notice_label, lv_pct(100));              // 设置标签宽度为容器的100%
    lv_label_set_long_mode(notice_label, LV_LABEL_LONG_WRAP); // 自动换行

    // 首先尝试从文件加载公告
    if(!load_notices_from_file()) {
        LV_LOG_USER("Failed to load notices from file, using default notices");
        // 如果加载失败，使用默认的公告
        static const char * default_notices[] = {"周一至周五 10:00前场次享8折优惠", "会员购票满3张送爆米花一份",
                                                 "学生凭学生证享7折优惠", "扫码关注公众号立减5元"};
        // 拼接默认公告内容
        char notice_text[512] = "";
        for(int i = 0; i < sizeof(default_notices) / sizeof(default_notices[0]); i++) {
            strcat(notice_text, "• ");
            strcat(notice_text, default_notices[i]);
            strcat(notice_text, "\n\n");
        }
        lv_label_set_text(notice_label, notice_text);
    } else {
        // 使用从文件加载的公告
        char notice_text[1024] = "";
        for(int i = 0; i < notice_count; i++) {
            strcat(notice_text, "• ");
            strcat(notice_text, notice_texts[i]);
            strcat(notice_text, "\n\n");
        }
        lv_label_set_text(notice_label, notice_text);
    }

    return notice_container;
}
// 创建自助购票区域
static lv_obj_t * create_ticket_section(lv_obj_t * parent)
{
    lv_obj_t * ticket_container = lv_obj_create(parent);
    if(!ticket_container) return NULL;

    lv_obj_set_size(ticket_container, 310, 220);

    lv_obj_set_style_bg_color(ticket_container, lv_color_hex(0xf7f9fc), 0);
    lv_obj_set_style_border_width(ticket_container, 1, 0);
    lv_obj_set_style_border_color(ticket_container, lv_color_hex(0xeeeeee), 0);
    lv_obj_clear_flag(ticket_container, LV_OBJ_FLAG_SCROLLABLE); // 取消滚动
    lv_obj_set_style_radius(ticket_container, 10, 0);            // 设置圆角
    lv_obj_set_style_pad_all(ticket_container, 0, 0);            // 设置内边距

    // 添加标题
    create_title_section(ticket_container, "自助服务");

    // 创建功能按钮
    create_function_button(ticket_container, "自助购票", 0, -30, 180, 50); // 调整位置
    create_function_button(ticket_container, "自助取票", 0, 30, 180, 50);  // 调整位置

    return ticket_container;
}

// 创建主菜单界面

void create_main_menu(void)
{
    // 如果菜单屏幕已存在，直接显示它
    if(menu_screen) {
        if(menu_screen_hidden) {
            lv_obj_clear_flag(menu_screen, LV_OBJ_FLAG_HIDDEN);
            menu_screen_hidden = false;
        }
        // 恢复定时器
        if(time_timer) {
            lv_timer_resume(time_timer);
        }
        lv_scr_load_anim(menu_screen, LV_SCR_LOAD_ANIM_FADE_ON, 300, 0, false);
        return;
    }

    // 1. 创建全屏背景
    menu_screen = lv_obj_create(NULL);
    if(!menu_screen) {
        LV_LOG_USER("Failed to create menu screen object");
        return;
    }

    lv_obj_set_size(menu_screen, 800, 480);
    lv_obj_set_style_bg_color(menu_screen, lv_color_hex(0xf5f5f5), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(menu_screen, LV_OPA_COVER, LV_PART_MAIN);
    lv_obj_clear_flag(menu_screen, LV_OBJ_FLAG_SCROLLABLE);

    // 2. 创建顶部信息栏
    lv_obj_t * top_bar = lv_obj_create(menu_screen);
    if(top_bar) {
        lv_obj_set_size(top_bar, 800, 40);
        lv_obj_align(top_bar, LV_ALIGN_TOP_LEFT, 0, 0);
        lv_obj_set_style_bg_color(top_bar, lv_color_hex(0x000000), 0);
        lv_obj_set_style_bg_opa(top_bar, LV_OPA_100, 0); // 纯黑色
        lv_obj_clear_flag(top_bar, LV_OBJ_FLAG_SCROLLABLE);

        // 影院标题
        lv_obj_t * label_cinema = lv_label_create(top_bar);
        if(label_cinema) {
            lv_obj_set_style_text_font(label_cinema, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
            lv_label_set_text(label_cinema, "栗子电影院");
            lv_obj_align(label_cinema, LV_ALIGN_LEFT_MID, 20, 0);
            lv_obj_set_style_text_color(label_cinema, lv_color_hex(0xffffff), LV_PART_MAIN);
        }

        // // 时间标签
        // label_time = lv_label_create(top_bar);
        // if(label_time) {
        //     lv_obj_set_style_text_font(label_time, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
        //     // lv_label_set_text(label_time, "加载中...");
        //     lv_obj_align(label_time, LV_ALIGN_RIGHT_MID, -20, 0);
        //     lv_obj_set_style_text_color(label_time, lv_color_hex(0xffffff), LV_PART_MAIN);
        // }

        // 启动时间更新定时器
        if(!time_timer) {
            time_timer = lv_timer_create(buy_tickets_update_time_timer, 1000, NULL);
        }
    }

    // 3. 创建内容容器 - 使用水平布局作为主容器
    lv_obj_t * main_container = lv_obj_create(menu_screen);
    if(main_container) {
        lv_obj_set_size(main_container, 800, 440);
        lv_obj_align(main_container, LV_ALIGN_BOTTOM_MID, 0, 0);
        lv_obj_set_style_bg_opa(main_container, LV_OPA_TRANSP, 0);
        lv_obj_set_style_border_width(main_container, 0, 0);
        lv_obj_clear_flag(main_container, LV_OBJ_FLAG_SCROLLABLE);

        // 主容器使用水平布局
        lv_obj_set_flex_flow(main_container, LV_FLEX_FLOW_ROW);
        lv_obj_set_flex_align(main_container,
                              LV_FLEX_ALIGN_START, // 主轴左对齐
                              LV_FLEX_ALIGN_START, // 交叉轴顶对齐
                              LV_FLEX_ALIGN_CENTER);
        lv_obj_set_style_pad_column(main_container, 10, 0); // 左右容器间距

        // 左侧影片区域 - 占大约2/3宽度
        lv_obj_t * film_container = lv_obj_create(main_container);
        lv_obj_set_size(film_container, 480, 440); // 宽度约为800的2/3
        lv_obj_set_style_bg_opa(film_container, LV_OPA_TRANSP, 0);
        lv_obj_set_style_border_width(film_container, 0, 0);
        lv_obj_clear_flag(film_container, LV_OBJ_FLAG_SCROLLABLE); // 取消滚动

        // 创建影片展示区域
        create_film_section(film_container);

        // 右侧容器 - 垂直布局，放置通知和票务区域，占大约1/3宽度 内边距为0
        lv_obj_t * right_container = lv_obj_create(main_container);
        lv_obj_set_size(right_container, 320, 440); // 宽度约为800的1/3
        lv_obj_set_style_bg_opa(right_container, LV_OPA_TRANSP, 0);
        lv_obj_set_style_border_width(right_container, 0, 0);
        lv_obj_clear_flag(right_container, LV_OBJ_FLAG_SCROLLABLE); // 取消滚动

        // 右侧容器使用垂直布局
        lv_obj_set_flex_flow(right_container, LV_FLEX_FLOW_COLUMN);
        lv_obj_set_flex_align(right_container,
                              LV_FLEX_ALIGN_CENTER, // 主轴居中
                              LV_FLEX_ALIGN_START,  // 交叉轴顶对齐
                              LV_FLEX_ALIGN_CENTER);
        lv_obj_set_style_pad_row(right_container, 10, 0); // 上下元素间距

        // 让右侧两个元素平分高度
        lv_obj_set_style_flex_grow(right_container, 1, 0);

        // 创建通知和票务区域
        create_notice_section(right_container);
        create_ticket_section(right_container);
    }

    // 5. 创建返回按钮 返回first_screen  使用绝对位置
    lv_obj_t * exit_btn = create_function_button(menu_screen, "退出登录", 0, 0, 100, 40);
    if(exit_btn) {
        lv_obj_align(exit_btn, LV_ALIGN_BOTTOM_RIGHT, -20, -20); // 右下角，带边距
    }

    // 加载菜单界面
    lv_scr_load_anim(menu_screen, LV_SCR_LOAD_ANIM_FADE_ON, 300, 0, false);
}